﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using personalplaner.business.extensions;

namespace personalplaner.business.dienstplan.plancollection
{
	public class PersonRow : ListBase<PlanItem>, IDienstPlanRow
    {
		public static Color? HolidayColor { get; set; }

		public void Initialize(DateTime beginDate, IList<PlanItem> items = null)
        {
			//int cnt = DateTime.DaysInMonth(beginDate.Year, beginDate.Month);
			//Items = new List<PlanItem>();
			//for (int i = 0; i < cnt; i++)
			//    Items.Add(new PlanItem { Datum = new DateTime(beginDate.Year, beginDate.Month, i + 1) });

			//foreach (var planitem in Items)
			//{
			//    foreach (var itm in items)
			//    {
			//        if (itm.Datum == planitem.Datum)
			//        {
			//            planitem.ID = itm.ID;
			//            planitem.Name = itm.Name;
			//            items.Remove(itm);
			//            break;
			//        }
			//    }
			//}

			int cnt = DateTime.DaysInMonth(beginDate.Year, beginDate.Month);
            for (int i = 0; i < cnt; i++)
                Add(new PlanItem
                {
                    Datum = new DateTime(beginDate.Year, beginDate.Month, i + 1),
                    Color = RowColor,
                    AbteilungId = this.AbteilungId,
                    PrsId = this.PrsId
                });



			if (items == null)
				return;

            while (items.Count > 0)
            {
                var itm = items[0];
                foreach (var planitem in this)
                {
                    if (itm.Datum == planitem.Datum)
                    {
                        if (itm.Color == null || itm.Color == Colors.Transparent)
                            itm.Color = planitem.Color;

                        itm.PrsId = PrsId;
                        itm.AbteilungId = AbteilungId;

						itm.BackgroundColor = itm.DienstType == enums.enDienstType.Ferien ? HolidayColor : null;

                        Replace(planitem, itm);
                        items.Remove(itm);
                        break;
                    }
                }
            }
        }

        string _displayname;
        public override string Name
        {
            get
            {
                if (string.IsNullOrEmpty(_displayname))
                    return string.Format("{0} {1}", Nachname, Vorname);
                return _displayname;
            }
            set
            {
                _displayname = value;
                RaisePropertyChanged("Name");
            }
        }

        public int PrsId
        {
            get
            {
                return ID;
            }
            set
            {
                ID = value;
                RaisePropertyChanged("PrsId");
            }
        }

        int _abteilungid;
        public int AbteilungId
        {
            get
            {
                return _abteilungid;
            }
            set
            {
                _abteilungid = value;
                RaisePropertyChanged("AbteilungId");
            }
        }

        string _abteilung;
        public string Abteilung
        {
            get
            {
                return _abteilung;
            }
            set
            {
                _abteilung = value;
                RaisePropertyChanged("Abteilung");
            }
        }

        string _vorname;
        public string Vorname
        {
            get { return _vorname; }
            set
            {
                _vorname = value;
                RaisePropertyChanged("Vorname");
            }
        }

        string _nachname;
        public string Nachname
        {
            get { return _nachname; }
            set
            {
                _nachname = value;
                RaisePropertyChanged("Nachname");
            }
        }

        int _planSchemaId;
        public int PlanSchemaId
        {
            get
            {
                return _planSchemaId;
            }
            set
            {
                _planSchemaId = value;
                RaisePropertyChanged("PlanSchemaId");
            }
        }

		int? _pensum;
		public int? Pensum
		{
			get { return _pensum; }
			set
			{
				_pensum = value;
				RaisePropertyChanged("Pensum");
			}
		}

		#region Stunden Soll/Haben

		double? _vortrag;
		public double? Vortrag
		{
			get { return _vortrag; }
			set
			{
				_vortrag = value;
				RaisePropertyChanged("Vortrag");
				RaisePropertyChanged("Differenz");
			}
		}

		double? _soll;
		public double? Soll
		{
			get { return _soll; }
			set
			{
				_soll = value;
				RaisePropertyChanged("Soll");
				RaisePropertyChanged("Differenz");
			}
		}

		double? _haben;
		public double? Haben
		{
			get { return _haben; }
			set
			{
				_haben = value;
				RaisePropertyChanged("Haben");
				RaisePropertyChanged("Differenz");
			}
		}

		double? _anpassung;
		public double? Anpassung
		{
			get { return _anpassung; }
			set
			{
				_anpassung = value;
				RaisePropertyChanged("Anpassung");
				RaisePropertyChanged("Differenz");
			}
		}

		public double? Differenz
		{
			get
			{
				if (Vortrag != null && Haben != null && Soll != null && Anpassung != null)
					return ((double) Vortrag + ((double) Haben - (double) Soll) + (double) Anpassung).Round();

				return 0.0;
			}
		}

		#endregion

		#region Coloring

		Color _color;
        public Color RowColor
        {
            get
            {
                return _color;
            }
            set
            {
                _color = value;
				RaisePropertyChanged("RowColor");
            }
        }

		Color _headercolor;
		public Color HeaderColor
		{
			get
			{
				return _headercolor;
			}
			set
			{
				_headercolor = value;
				RaisePropertyChanged("HeaderColor");
			}
		}

		#endregion
	}
}
